﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BZLib.Global;
using BZLib.Network;

namespace BZLib
{
    namespace Flags
    {
        public enum FlagStatus
        {
            NonExistant = 0,
            OnGround = 1,
            OnTank = 2,
            InAir = 3,
            Coming = 4,
            Going = 5
        }

        public enum FlagEndurance
        {
            Normal = 0,
            Unstable = 1,
            Sticky = 2
        }

        public enum FlagQuality
        {
            Good = 0,
            Bad = 1
        }

        public enum FlagShotType
        {
            Normal = 0,
            Special = 1
        }

        public class Flag: IPackable, IUnpackable
        {
            public int ID;
            public FlagType Type;
            public FlagStatus Status;
            public FlagEndurance Endurance;
            public int Owner;
            public Point Position;
            public Point LaunchPosition;
            public Point LandingPosition;
            public float FlightTime;
            public float FlightEnd;
            public float InitialVelocity;

            public Flag() { }

            public Flag(FlagType type)
            {
                Type = type;
                ID = -1;
                Status = FlagStatus.Coming;
                Endurance = type.Endurance;
                Owner = -1;
                Position = new Point();
                LaunchPosition = new Point();
                LandingPosition = new Point();
                FlightTime = -1;
                FlightEnd = -1;
                InitialVelocity = -1;
            }

            BinaryString IPackable.Pack()
            {
                BinaryString str = new BinaryString();
                str.PackUShort((ushort)ID);
                str.PackString(Type.Abbr, 2);
                str.PackUShort((ushort)Status);
                str.PackUShort((ushort)Endurance);
                str.PackUByte((byte)Owner);
                str.Pack(Position);
                str.Pack(LaunchPosition);
                str.Pack(LandingPosition);
                str.PackFloat(FlightTime);
                str.PackFloat(FlightEnd);
                str.PackFloat(InitialVelocity);

                return str;
            }

            BinaryString IUnpackable.Unpack(BinaryString str)
            {
                ID = str.UnpackUShort();
                Type = FlagList.GetByAbbr(str.UnpackString(2));
                Status = (FlagStatus)str.UnpackUShort();
                Endurance = (FlagEndurance)str.UnpackUShort();
                Owner = str.UnpackUByte();
                str.Unpack(Position);
                str.Unpack(LaunchPosition);
                str.Unpack(LandingPosition);
                FlightTime = str.UnpackFloat();
                FlightEnd = str.UnpackFloat();
                InitialVelocity = str.UnpackFloat();

                return str;
            }
        }
    }
}
